// pages/attestation/attestation.js
const app = getApp(), core = require('../../utils/core'), WeValidator = require('../../utils/we-validator');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    audit_status: '',
    audit_info: {},
    license_type: null,
    license_types: ['永久', '日期选择'],
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      'audit_info.valid_date': e.detail.value,
    })
  },
   bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
    if(e.detail.value==0) {
      this.setData({'audit_info.valid_date': '永久'});
    }else {
      this.setData({'audit_info.valid_date': ''});
    }
    this.setData({
      license_type: e.detail.value,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let page = this;
    this.setData({userinfo: app.getUserinfo()});
    core.get({
      url:'/enterprise/audit_info',
      success(ret, response) {
        page.setData(ret.data);
        if(ret.data.audit_info.valid_date=='') {
          page.setData({license_type:null});
        }else if(ret.data.audit_info.valid_date=='永久') {
          page.setData({license_type: 0});
        }else {
          page.setData({license_type: 1});
        }
        // if(ret.data.audit_status==1 && page.data.userinfo.group_model.audit_status!=1) {
        //   //审核通过， 重启到首页
        //   page.data.userinfo.group_model.audit_status = 1;
        //   app.setUserinfo(page.data.userinfo);
        //   wx.switchTab({url:'/pages/indexpage/indexpage'});
        //   return;
        // }
        // if(ret.data.audit_status==2) {
        //   //待审核  轮询结果
        //   page.timer = setInterval(page._fetch, 30000);
        // }
      }
    });
    // app.eventBus.on(app.Events.UserinfoUpdate, this.eventOnUserinfoUpdate, this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    try {
      // if (this.data.userinfo.group_model.audit_status!=1) {
      //   wx.hideHomeButton();
      // }
    }catch (e) {
      // wx.hideHomeButton();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if(this.timer)
      clearInterval(this.timer);
    // app.eventBus.off(app.Events.UserinfoUpdate, this.eventOnUserinfoUpdate);
  },

  upload() {
    // if(![0,3].includes(parseInt(this.data.audit_status)))
    //   return;
    let page = this;
    wx.chooseImage({
      count:1,
      success: res => {
        let file = res.tempFiles[0];
        if(file.size > 10*1024*1024) {
          wx.showToast({title:'文件大小最多10M',icon:'none'});
          return;
        }
        core.uploadFile({
          filePath: file.path,
          success(ret, response) {
            page.setData({'audit_info.license_img': ret.data.url});
          }
        })
      }
    });
  },
  submit(event) {
    // if(![0,3].includes(parseInt(this.data.audit_status)))
    //   return;
    let data = event.detail.value, page = this;
    let validator = new WeValidator({
      rules: {
        license_img: {required: true},
        company_name: {required: true},
        license_no: {required: true},
        valid_date: {required: true}
      },
      messages: {
        license_img: {required:'请上传营业执照'},
        company_name: {required: '请填写公司全称'},
        license_no: {required: '请填写注册号'},
        valid_date: {required: '请选择有效日期'}
      }
    });
    if(!validator.checkData(data)) {
      return;
    }
    core.post({
      url: '/enterprise/audit_info',
      data: data,
      success(ret, response) {
        page.setData(ret.data);
        page.data.userinfo.group_model.audit_status = ret.data.audit_status;
        app.setUserinfo(page.data.userinfo);
        // page.timer = setInterval(page._fetch, 3000);
        // if(getCurrentPages().length>1)
        wx.navigateBack();
        // else
        //   wx.redi
      }
    })
  },
  // eventOnUserinfoUpdate: function(userinfo) {
  //   if(userinfo.group_id!=this.data.userinfo.group_id) {
  //     wx.reLaunch({url:'/pages/indexpage/indexpage'});
  //   }
  // },
  _fetch() {
    let page = this;
    core.get({
      url:'/enterprise/audit_info',
      loading:false,
      success(ret, response) {
        if(ret.data.audit_status==1) {
          page.data.userinfo.group_model.audit_status = ret.data.audit_status;
          app.setUserinfo(page.data.userinfo);
          wx.switchTab({url:'/pages/indexpage/indexpage'});
        }else if(ret.data.audit_status!=2) {
          clearInterval(page.timer);
          page.timer = 0;
          page.setData(ret.data);
          if(ret.data.audit_info.valid_date=='') {
            page.setData({license_type:null});
          }else if(ret.data.audit_info.valid_date=='永久') {
            page.setData({license_type: 0});
          }else {
            page.setData({license_type: 1});
          }
        }
      }
    })
  },

});
